#pragma once

#include <iostream>
#include <assert.h>

namespace lw
{
	template<class T>
	class vector
	{
		typedef T* iterator;
		typedef const T* const_iterator;
	public:
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}
		vector() {}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		vector(const std::initializer_list<T>& il)
		{
			reserve(il.size());
			for (const auto& e : il)
				push_back(e);
		}
		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (const auto& e : v)
				push_back(e);
		}
		vector(vector<T>&& v)
		{
			swap(v);
		}
		vector(size_t n, const T& val)
		{
			resize(n, val);
		}
		vector(int n, const T& val)
		{
			resize(n, val);
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		vector<T>& operator=(vector<T>&& v)
		{
			swap(v);
			return *this;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				int sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (int i = 0; i < sz; i++)
						tmp[i] = std::move(_start[i]);
					delete[] _start;
				}
				_start = tmp;
				_finish = tmp + sz;
				_end_of_storage = tmp + n;
			}
		}
		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				for (int i = size(); i < n; i++)
					_start[i] = val;
				_finish = _start + n;
			}
		}
		void push_back(const T& val)
		{
			if (size() == capacity())
			{
				reserve(size() == 0 ? 4 : 2 * size());
			}
			*_finish++ = val;
		}
		void pop_back()
		{
			assert(!empty());

			--_finish;
		}
		iterator insert(iterator pos, const T& val)
		{
			assert(pos >= _start && pos <= _finish);
			if (size() == capacity())
			{
				int len = pos - _start;
				reserve(size() == 0 ? 4 : 2 * size());
				pos = _start + len;
			}
			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator tmp = pos;
			while (tmp < _finish)
			{
				*tmp = *(tmp + 1);
				tmp++;
			}
			_finish--;
			return pos;
		}
		void erase(iterator begin, iterator end)
		{
			iterator next = end + 1;
			while (next < _finish)
			{
				*begin = *next;
				begin++;
				next++;
			}
			_finish = begin;
		}


		T& operator[](size_t n)
		{
			assert(n < size());

			return _start[n];
		}
		const T& operator[](size_t n) const
		{
			assert(n < size());

			return _start[n];
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}
		void clear()
		{
			_finish = _start;
		}
		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}
		bool empty() const
		{
			return size() == 0;
		}

	private:
		T* _start = nullptr;
		T* _finish = nullptr;
		T* _end_of_storage = nullptr;
	};
}